﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Protal.Models;
using AutoMapper;
using Microhard.Protal.DataAccess;
using System.Web;
using Microhard.Core.Extensions;

namespace Microhard.Protal.DTO
{
    public static class ProtalMapExtension
    {
        #region Category
        public static CategoryModel ToModel(this Microhard_Category entity)
        {
            var model = Mapper.Map<CategoryModel>(entity);
            if (entity.Microhard_Product.Count > 0)
            {
                model.Products = entity.Microhard_Product.Select(p => p.ToModel()).ToList();
            }
            else
            {
                model.Products = new List<ProductModel>();
            }
            model.DescriptionContent = HttpUtility.HtmlDecode(entity.DescriptionContent.IndentHtml());
            return model;
        }

        public static Microhard_Category ToEntity(this CategoryModel model)
        {
            return Mapper.Map<Microhard_Category>(model);
        }
        #endregion

        #region SiteInfo
        public static SiteInfoModel ToModel(this Microhard_SiteInfo entity)
        {
            var model = Mapper.Map<SiteInfoModel>(entity);
            model.Value = HttpUtility.HtmlDecode(model.Value.IndentHtml());
            return model;
        }

        public static Microhard_SiteInfo ToEntity(this SiteInfoModel model)
        {
            return Mapper.Map<Microhard_SiteInfo>(model);
        }
        #endregion

        #region Product
        public static ProductModel ToModel(this Microhard_Product entity)
        {
            var model = Mapper.Map<ProductModel>(entity);
            model.Decription = HttpUtility.HtmlDecode(entity.Decription.IndentHtml());
            model.CategoryName = entity.Microhard_Category.Name;
            return model;
        }

        public static Microhard_Product ToEntity(this ProductModel model)
        {
            return Mapper.Map<Microhard_Product>(model);
        }
        #endregion

        #region Article
        public static ArticleModel ToModel(this Microhard_Article entity)
        {
            var model = Mapper.Map<ArticleModel>(entity);
            model.ArticleContent = HttpUtility.HtmlDecode(entity.ArticleContent.IndentHtml());
            return model;
        }

        public static Microhard_Article ToEntity(this ArticleModel model)
        {
            return Mapper.Map<Microhard_Article>(model);
        }
        #endregion

        #region Attachment
        public static AttachmentModel ToModel(this Attachment entity)
        {
            var model = Mapper.Map<AttachmentModel>(entity);
            return model;
        }

        public static Attachment ToEntity(this AttachmentModel model)
        {
            return Mapper.Map<Attachment>(model);
        }
        #endregion

        #region Service
        public static ServiceModel ToModel(this Microhard_CustomerService entity)
        {
            var model = Mapper.Map<ServiceModel>(entity);
            return model;
        }

        public static Microhard_CustomerService ToEntity(this ServiceModel model)
        {
            return Mapper.Map<Microhard_CustomerService>(model);
        }
        #endregion
    }
}
